package homework.organizer;

import homework.entities.Constants;
import homework.entities.Tema;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class Homeworks extends Activity {

	private ArrayList<String> temeList;
	private ArrayAdapter<String> adapter;
	private ArrayList<Tema> teme;
	
	/**
	 * this class is also used to load some home works. instances of this class that are not run with startActivity
	 * will not load any views. 
	 */
	public Homeworks(){
		
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homework_list_layout);
		
		temeList = new ArrayList<String>();
		teme = new ArrayList<Tema>();
		
		ListView listView = (ListView)findViewById(R.id.homeworkList);
		
		adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.homework_list_item, R.id.label, temeList);
		
		listView.setAdapter(adapter);
		
		
		listView.setOnItemClickListener(this.clickListener);
		
		
		populateWithHomework();
		
	}
	
	
	public void populateWithHomework(){
		
		if(teme == null) teme = new ArrayList<Tema>();
		if(temeList == null) temeList = new ArrayList<String>();
		
		
		File file = new File(Constants.TEMA_SAVE_FILE);
		
		BufferedReader bf = null;
		
		try {
			
			bf = new BufferedReader(new FileReader(file));
			String line;
			while ((line = bf.readLine()) != null){
				
				String[] detalii = line.split("\\$");
				
				Tema tema = new Tema(detalii[0], detalii[1]);
				tema.setSubject(detalii[2]);
				tema.setDeadline(detalii[3]);
				tema.setNotes(detalii[4]);
				
				teme.add(tema);
				temeList.add(tema.getDisplayName());
				
			}
		
			adapter.notifyDataSetChanged();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
		
	}
	
	public ArrayList<Tema> getHomeworks(){
		
		return teme;
		
	}
	
	private OnItemClickListener clickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {
			
			String selectedHomeworkDisplayName = temeList.get(pos);
			
			Tema selectedHomework = null;
			
			for(Tema t : teme){
				
				if(t.getDisplayName().equals(selectedHomeworkDisplayName)){
					
					selectedHomework = t;
					
				}
				
			}
			
			if(selectedHomework != null){
				
				Intent intent = new Intent(getApplicationContext(), HomeworkDetails.class);
				Bundle data = new Bundle();
				data.putSerializable("homework", selectedHomework);
				intent.putExtra("data", data);
				
				startActivity(intent);
				
			}
			
			
		}
	};
	
	
}
